using System;
using System.Collections.Generic;
using Rilisoft.MiniJson;
using UnityEngine;

public class LeaderBoardParser
{
	private const int COUNT_PLAYER_IN_COMMON_LEADER_BOARD = 100;

	private const int FAKE_PLACE = 998;

	private static LeaderBoardParser instance;

	private Dictionary<string, Dictionary<TypeGame, List<LeaderInfo>>> _dictionaryTypeGameLeaders = new Dictionary<string, Dictionary<TypeGame, List<LeaderInfo>>>();

	private Dictionary<string, Dictionary<TypeGame, bool>> _isParseDictLeaderBoardByTypeGame = new Dictionary<string, Dictionary<TypeGame, bool>>();

	private bool _isParseLeaderBoardByTypeGame;

	private Dictionary<LeaderBoardTypeGameUI.TypeLeaderBoardByTypeGame, Dictionary<TypeGame, LeaderInfo>> _dictionaryTypeGameMyPlayer = new Dictionary<LeaderBoardTypeGameUI.TypeLeaderBoardByTypeGame, Dictionary<TypeGame, LeaderInfo>>();

	private List<LeaderInfo> _topPlayer = new List<LeaderInfo>();

	private List<LeaderInfo> _topInLeague = new List<LeaderInfo>();

	private List<LeaderInfo> _topFriends = new List<LeaderInfo>();

	private Dictionary<string, LeaderInfo> _myPlayerInTops = new Dictionary<string, LeaderInfo>();

	private bool _isParseLeaderBoardCommon;

	private LeaderBoardParser()
	{
	}

	public static LeaderBoardParser GetInstance()
	{
		if (instance == null)
		{
			instance = new LeaderBoardParser();
		}
		return instance;
	}

	public static void ResetInstance()
	{
		instance = null;
	}

	public void ParseLeaderBoardNew(string typeLeaderBoard, TypeGame typeGame)
	{
		if (_isParseDictLeaderBoardByTypeGame.ContainsKey(typeLeaderBoard) && _isParseDictLeaderBoardByTypeGame[typeLeaderBoard].ContainsKey(typeGame))
		{
			return;
		}
		string @string = PlayerPrefs.GetString("keyLeaders" + typeLeaderBoard + LeaderBoardServer.TypeForLeaderboard(typeGame));
		List<object> list = Json.Deserialize(@string) as List<object>;
		if (list == null)
		{
			return;
		}
		List<LeaderInfo> list2 = new List<LeaderInfo>();
		for (int i = 0; i < list.Count; i++)
		{
			LeaderInfo item = default(LeaderInfo);
			Dictionary<string, object> dictionary = list[i] as Dictionary<string, object>;
			if (dictionary.ContainsKey("nick"))
			{
				item.nickName = dictionary["nick"].ToString();
			}
			if ((!dictionary.ContainsKey("scores") || int.TryParse(dictionary["scores"].ToString(), out item.stars)) && (!dictionary.ContainsKey("place") || int.TryParse(dictionary["place"].ToString(), out item.place)))
			{
				if (dictionary.ContainsKey("id"))
				{
					item.idPlayer = dictionary["id"].ToString();
				}
				item.me = false;
				list2.Add(item);
			}
		}
		if (!_dictionaryTypeGameLeaders.ContainsKey(typeLeaderBoard))
		{
			Dictionary<TypeGame, List<LeaderInfo>> dictionary2 = new Dictionary<TypeGame, List<LeaderInfo>>();
			dictionary2.Add(typeGame, list2);
			_dictionaryTypeGameLeaders.Add(typeLeaderBoard, dictionary2);
		}
		else
		{
			_dictionaryTypeGameLeaders[typeLeaderBoard].Add(typeGame, list2);
		}
		if (!_isParseDictLeaderBoardByTypeGame.ContainsKey(typeLeaderBoard))
		{
			Dictionary<TypeGame, bool> dictionary3 = new Dictionary<TypeGame, bool>();
			dictionary3.Add(typeGame, true);
			_isParseDictLeaderBoardByTypeGame.Add(typeLeaderBoard, dictionary3);
		}
		else
		{
			_isParseDictLeaderBoardByTypeGame[typeLeaderBoard].Add(typeGame, true);
		}
	}

	public void ParseLeaderBoard()
	{
		if (_isParseLeaderBoardByTypeGame)
		{
			return;
		}
		string @string = PlayerPrefs.GetString("keyLeaders");
		if (string.IsNullOrEmpty(@string))
		{
			return;
		}
		Dictionary<string, object> dictionary = Json.Deserialize(@string) as Dictionary<string, object>;
		if (dictionary == null)
		{
			return;
		}
		string[] array = new string[2] { "top_stars", "friends_stars" };
		_dictionaryTypeGameLeaders.Clear();
		string[] array2 = array;
		foreach (string key in array2)
		{
			if (!dictionary.ContainsKey(key))
			{
				continue;
			}
			Dictionary<string, object> dictionary2 = dictionary[key] as Dictionary<string, object>;
			if (dictionary2 == null)
			{
				continue;
			}
			Dictionary<TypeGame, List<LeaderInfo>> dictionary3 = new Dictionary<TypeGame, List<LeaderInfo>>();
			foreach (object value in Enum.GetValues(typeof(TypeGame)))
			{
				int num = LeaderBoardServer.TypeForLeaderboard((TypeGame)(int)value);
				if (!dictionary2.ContainsKey(num.ToString()))
				{
					continue;
				}
				List<object> list = dictionary2[num.ToString()] as List<object>;
				if (list == null)
				{
					continue;
				}
				List<LeaderInfo> list2 = new List<LeaderInfo>();
				for (int j = 0; j < list.Count; j++)
				{
					LeaderInfo item = default(LeaderInfo);
					Dictionary<string, object> dictionary4 = list[j] as Dictionary<string, object>;
					if (dictionary4.ContainsKey("nick"))
					{
						item.nickName = dictionary4["nick"].ToString();
					}
					if ((!dictionary4.ContainsKey("scores") || int.TryParse(dictionary4["scores"].ToString(), out item.stars)) && (!dictionary4.ContainsKey("place") || int.TryParse(dictionary4["place"].ToString(), out item.place)))
					{
						if (dictionary4.ContainsKey("id"))
						{
							item.idPlayer = dictionary4["id"].ToString();
						}
						item.me = false;
						list2.Add(item);
					}
				}
				dictionary3.Add((TypeGame)(int)value, list2);
			}
			_dictionaryTypeGameLeaders.Add(key, dictionary3);
		}
		_isParseLeaderBoardByTypeGame = true;
	}

	private string TypeLeaderBoardToString(LeaderBoardTypeGameUI.TypeLeaderBoardByTypeGame typeLeaderBoard)
	{
		switch (typeLeaderBoard)
		{
		case LeaderBoardTypeGameUI.TypeLeaderBoardByTypeGame.topAll:
			return "top_stars";
		case LeaderBoardTypeGameUI.TypeLeaderBoardByTypeGame.topFriends:
			return "friends_stars";
		default:
			return string.Empty;
		}
	}

	public List<LeaderInfo> GetLeadersByTypeGame(TypeGame typeGame, LeaderBoardTypeGameUI.TypeLeaderBoardByTypeGame typeLeaderBoard)
	{
		List<LeaderInfo> list = new List<LeaderInfo>();
		string text = TypeLeaderBoardToString(typeLeaderBoard);
		if (string.IsNullOrEmpty(text))
		{
			return list;
		}
		ParseLeaderBoardNew(text, typeGame);
		ParsePlayer();
		if (_dictionaryTypeGameLeaders.ContainsKey(text) && _dictionaryTypeGameLeaders[text].ContainsKey(typeGame))
		{
			list = _dictionaryTypeGameLeaders[text][typeGame];
		}
		LeaderInfo item = GetMyDefaultInfo();
		if (_dictionaryTypeGameMyPlayer.ContainsKey(typeLeaderBoard) && _dictionaryTypeGameMyPlayer[typeLeaderBoard].ContainsKey(typeGame))
		{
			item = _dictionaryTypeGameMyPlayer[typeLeaderBoard][typeGame];
		}
		LeaderInfo leaderInfo = default(LeaderInfo);
		for (int i = 0; i < list.Count; i++)
		{
			if (list[i].idPlayer == BCWServer.idPlayer)
			{
				leaderInfo = list[i];
				list.RemoveAt(i);
				break;
			}
		}
		List<LeaderInfo> list2 = new List<LeaderInfo>();
		int num = 0;
		bool flag = false;
		foreach (LeaderInfo item2 in list)
		{
			num++;
			if (item.stars >= item2.stars)
			{
				flag = true;
				break;
			}
			if (num >= 6)
			{
				break;
			}
		}
		num = 0;
		if (flag || list.Count <= 7)
		{
			int num2 = 1;
			bool flag2 = false;
			if (list.Count > 0)
			{
				foreach (LeaderInfo item3 in list)
				{
					if (num <= 5)
					{
						num++;
						LeaderInfo leaderInfo2 = default(LeaderInfo);
						if (item.stars >= item3.stars && !flag2)
						{
							item.place = num2;
							flag2 = true;
							list2.Add(item);
							num2++;
						}
						leaderInfo2 = item3;
						leaderInfo2.place = num2;
						num2++;
						list2.Add(leaderInfo2);
						continue;
					}
					break;
				}
			}
			if (!flag2)
			{
				item.place = num2;
				list2.Add(item);
			}
			return list2;
		}
		int num3 = 1;
		for (int j = 0; j < 4; j++)
		{
			LeaderInfo leaderInfo3 = default(LeaderInfo);
			leaderInfo3 = list[j];
			leaderInfo3.place = num3;
			num3++;
			list2.Add(leaderInfo3);
		}
		LeaderInfo leaderInfo4 = default(LeaderInfo);
		int stars = UnityEngine.Random.Range(item.stars + 1, list2[2].stars - 1);
		int num4 = UnityEngine.Random.Range(item.stars - 50, item.stars - 1);
		num4 = ((num4 > 0) ? num4 : 0);
		int num5 = UnityEngine.Random.Range(4, list.Count - 1);
		int num6 = UnityEngine.Random.Range(4, list.Count - 1);
		if (num5 == num6)
		{
			num6 = UnityEngine.Random.Range(4, list.Count - 1);
		}
		leaderInfo4 = list[num5];
		leaderInfo4.stars = stars;
		leaderInfo4.place = item.place - 1;
		list2.Add(leaderInfo4);
		list2.Add(item);
		leaderInfo4 = default(LeaderInfo);
		leaderInfo4 = list[num6];
		leaderInfo4.stars = num4;
		leaderInfo4.place = item.place + 1;
		list2.Add(leaderInfo4);
		return list2;
	}

	private LeaderInfo GetMyDefaultInfo()
	{
		LeaderInfo result = default(LeaderInfo);
		result.stars = 0;
		result.place = 998;
		result.nickName = BCWStorage.Get<string>(settings.keyCryptoName);
		result.idPlayer = BCWServer.idPlayer;
		result.me = true;
		return result;
	}

	private void ParsePlayer()
	{
		_dictionaryTypeGameMyPlayer.Clear();
		string @string = PlayerPrefs.GetString("keyPlayerInfo");
		if (string.IsNullOrEmpty(@string))
		{
			return;
		}
		Dictionary<string, object> dictionary = Json.Deserialize(@string) as Dictionary<string, object>;
		if (dictionary == null || !dictionary.ContainsKey("me"))
		{
			return;
		}
		Dictionary<string, object> dictionary2 = dictionary["me"] as Dictionary<string, object>;
		if (dictionary2 == null)
		{
			return;
		}
		_dictionaryTypeGameMyPlayer.Add(LeaderBoardTypeGameUI.TypeLeaderBoardByTypeGame.topAll, new Dictionary<TypeGame, LeaderInfo>());
		_dictionaryTypeGameMyPlayer.Add(LeaderBoardTypeGameUI.TypeLeaderBoardByTypeGame.topFriends, new Dictionary<TypeGame, LeaderInfo>());
		foreach (object value3 in Enum.GetValues(typeof(TypeGame)))
		{
			LeaderInfo value = default(LeaderInfo);
			LeaderInfo value2 = default(LeaderInfo);
			int num = LeaderBoardServer.TypeForLeaderboard((TypeGame)(int)value3);
			if (!dictionary2.ContainsKey(num.ToString()))
			{
				continue;
			}
			Dictionary<string, object> dictionary3 = dictionary2[num.ToString()] as Dictionary<string, object>;
			if (dictionary3 != null)
			{
				if (dictionary3.ContainsKey("scores"))
				{
					int result = 0;
					int.TryParse(dictionary3["scores"].ToString(), out result);
					value.stars = result + LeaderBoardStarsStorage.GetInstance().GetStarsByTypeGame((TypeGame)(int)value3);
					value2.stars = result + LeaderBoardStarsStorage.GetInstance().GetStarsByTypeGame((TypeGame)(int)value3);
				}
				value.nickName = BCWStorage.Get<string>(settings.keyCryptoName);
				value.idPlayer = BCWServer.idPlayer;
				value.me = true;
				value2.nickName = BCWStorage.Get<string>(settings.keyCryptoName);
				value2.idPlayer = BCWServer.idPlayer;
				value2.me = true;
				if (dictionary3.ContainsKey("place"))
				{
					int.TryParse(dictionary3["place"].ToString(), out value.place);
					_dictionaryTypeGameMyPlayer[LeaderBoardTypeGameUI.TypeLeaderBoardByTypeGame.topAll].Add((TypeGame)(int)value3, value);
				}
				if (dictionary3.ContainsKey("among_friends"))
				{
					int.TryParse(dictionary3["among_friends"].ToString(), out value2.place);
					_dictionaryTypeGameMyPlayer[LeaderBoardTypeGameUI.TypeLeaderBoardByTypeGame.topFriends].Add((TypeGame)(int)value3, value2);
				}
			}
		}
	}

	public void ParseLeaderBoardCommon()
	{
		if (_isParseLeaderBoardCommon)
		{
			return;
		}
		string @string = PlayerPrefs.GetString("keyLeadersCommon");
		if (!string.IsNullOrEmpty(@string))
		{
			ParseMyPlayerForLeaderBoardCommon();
			Dictionary<string, object> dictionary = Json.Deserialize(@string) as Dictionary<string, object>;
			if (dictionary != null)
			{
				_ParseDictionary("top_stars", dictionary, _topPlayer);
				_ParseDictionary("league_stars", dictionary, _topInLeague);
				_ParseDictionary("friends_stars", dictionary, _topFriends);
				_isParseLeaderBoardCommon = true;
			}
		}
	}

	private void _ParseDictionary(string responesKey, Dictionary<string, object> dataBase, List<LeaderInfo> top)
	{
		top.Clear();
		if (!dataBase.ContainsKey(responesKey))
		{
			return;
		}
		List<object> list = dataBase[responesKey] as List<object>;
		bool flag = false;
		foreach (object item4 in list)
		{
			if (top.Count >= 100)
			{
				break;
			}
			LeaderInfo item = _ParseLeader(item4 as Dictionary<string, object>);
			if (!(item.idPlayer == BCWServer.idPlayer))
			{
				if (!flag && _myPlayerInTops.ContainsKey(responesKey) && _myPlayerInTops[responesKey].stars > item.stars)
				{
					LeaderInfo item2 = _myPlayerInTops[responesKey];
					item2.place = top.Count + 1;
					top.Add(item2);
					flag = true;
				}
				item.place = top.Count + 1;
				top.Add(item);
			}
		}
		if (!flag && _myPlayerInTops.ContainsKey(responesKey))
		{
			LeaderInfo item3 = _myPlayerInTops[responesKey];
			if (top.Count < 100)
			{
				item3.place = top.Count + 1;
			}
			top.Add(item3);
		}
	}

	public void ParseMyPlayerForLeaderBoardCommon()
	{
		_myPlayerInTops.Clear();
		string[] array = new string[3] { "top_stars", "league_stars", "friends_stars" };
		string @string = PlayerPrefs.GetString("keyPlayerCommonInfo");
		if (string.IsNullOrEmpty(@string))
		{
			return;
		}
		Dictionary<string, object> dictionary = Json.Deserialize(@string) as Dictionary<string, object>;
		if (dictionary == null)
		{
			return;
		}
		string[] array2 = array;
		foreach (string key in array2)
		{
			LeaderInfo myDefaultInfo = GetMyDefaultInfo();
			if (!dictionary.ContainsKey(key))
			{
				_myPlayerInTops.Add(key, myDefaultInfo);
				continue;
			}
			Dictionary<string, object> dictionary2 = dictionary[key] as Dictionary<string, object>;
			if (dictionary2 == null)
			{
				_myPlayerInTops.Add(key, myDefaultInfo);
				continue;
			}
			if (dictionary2.ContainsKey("scores"))
			{
				int.TryParse(dictionary2["scores"].ToString(), out myDefaultInfo.stars);
				myDefaultInfo.stars += LeaderBoardStarsStorage.GetInstance().GetAllStars();
			}
			if (dictionary2.ContainsKey("place"))
			{
				int.TryParse(dictionary2["place"].ToString(), out myDefaultInfo.place);
				if (myDefaultInfo.place <= 0)
				{
					myDefaultInfo.place = 998;
				}
			}
			_myPlayerInTops.Add(key, myDefaultInfo);
		}
	}

	public List<LeaderInfo> GetTopByTypeLeaderBoard(PanelLeaderBoardUI.TypeLeaderBoard typeLeaderBoard)
	{
		ParseLeaderBoardCommon();
		List<LeaderInfo> result = new List<LeaderInfo>();
		switch (typeLeaderBoard)
		{
		case PanelLeaderBoardUI.TypeLeaderBoard.TopPlayers:
			return _topPlayer;
		case PanelLeaderBoardUI.TypeLeaderBoard.TopLeague:
			return _topInLeague;
		case PanelLeaderBoardUI.TypeLeaderBoard.TopFriends:
			return _topFriends;
		default:
			return result;
		}
	}

	public LeaderInfo GetLeaderByType(PanelLeaderBoardUI.TypeLeaderBoard typeLeaderBoard)
	{
		LeaderInfo leaderInfo = default(LeaderInfo);
		leaderInfo.stars = 0;
		leaderInfo.place = 100;
		leaderInfo.nickName = BCWStorage.Get<string>(settings.keyCryptoName);
		leaderInfo.idPlayer = BCWServer.idPlayer;
		leaderInfo.me = true;
		switch (typeLeaderBoard)
		{
		case PanelLeaderBoardUI.TypeLeaderBoard.TopPlayers:
			return (!_myPlayerInTops.ContainsKey("top_stars")) ? leaderInfo : _myPlayerInTops["top_stars"];
		case PanelLeaderBoardUI.TypeLeaderBoard.TopLeague:
			return (!_myPlayerInTops.ContainsKey("league_stars")) ? leaderInfo : _myPlayerInTops["league_stars"];
		case PanelLeaderBoardUI.TypeLeaderBoard.TopFriends:
			return (!_myPlayerInTops.ContainsKey("friends_stars")) ? leaderInfo : _myPlayerInTops["friends_stars"];
		default:
			return leaderInfo;
		}
	}

	private LeaderInfo _ParseLeader(Dictionary<string, object> leaderObject)
	{
		LeaderInfo result = default(LeaderInfo);
		if (leaderObject.ContainsKey("name"))
		{
			string nickName = leaderObject["name"].ToString();
			result.nickName = nickName;
		}
		if (leaderObject.ContainsKey("scores"))
		{
			int.TryParse(leaderObject["scores"].ToString(), out result.stars);
		}
		if (leaderObject.ContainsKey("place"))
		{
			int.TryParse(leaderObject["place"].ToString(), out result.place);
		}
		if (leaderObject.ContainsKey("id"))
		{
			result.idPlayer = leaderObject["id"].ToString();
		}
		if (leaderObject.ContainsKey("rank"))
		{
			int result2 = 0;
			int.TryParse(leaderObject["rank"].ToString(), out result2);
			result.league = CupsManager.GetLeagueForIndex(result2);
		}
		result.me = result.idPlayer == BCWServer.idPlayer;
		return result;
	}
}
